package com.huidu.bitrade.controller.member;

import com.huidu.bitrade.result.page.PageCondition;
import com.huidu.bitrade.entity.MemberLoan;
import com.huidu.bitrade.pagination.PageResult;
import com.huidu.bitrade.query.MemberLoanPageQuery;
import com.huidu.bitrade.service.MemberLoanService;
import com.huidu.bitrade.util.MessageResult;
import com.huidu.bitrade.vo.MemberLoanVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Api("用户赎回审核")
@RestController
@Slf4j
@RequestMapping("loan")
public class MemberLoanController {

    @Autowired
    private MemberLoanService memberLoanService;

    @GetMapping("pageList")
    @ApiOperation("分页列表")
    public PageResult<MemberLoanVO> pageList(MemberLoanPageQuery mpPageQuery){
        return new PageResult<>(memberLoanService.pageList(PageCondition.page(mpPageQuery),mpPageQuery));
    }

    @PostMapping("audit")
    @ApiOperation("审核")
    public MessageResult audit(@RequestParam("status") Integer status,
                               @RequestParam("id") Long id,
                               @RequestParam(value = "description",required = false) String description){
        MemberLoan memberLoan = memberLoanService.getById(id);
        Assert.isTrue(Objects.nonNull(memberLoan),"信息不存在");
        boolean apply = false;
        switch (status){
            case 2://审核通过
                if(memberLoan.getStatus().getStatus()!=1){
                    return MessageResult.error("当前状态不可审核");
                }
                apply = memberLoanService.audit(memberLoan);
                break;
            case 3://赎回
                if(memberLoan.getStatus().getStatus()!=2){
                    return MessageResult.error("当前状态不可赎回");
                }
                apply = memberLoanService.redemptionAudit(memberLoan);
                break;
            case 4://审核拒绝
                if(memberLoan.getStatus().getStatus()!=1){
                    return MessageResult.error("当前状态不可审核");
                }
                apply = memberLoanService.reject(memberLoan,description);
                break;
        }
        return apply ? MessageResult.success() : MessageResult.error("操作失败");
    }
}
